import { View, Button, Text } from '@tarojs/components/';
import { set_address } from "../../actions/positioning";

import PropTypes from 'prop-types';
import Taro from "@tarojs/taro";
import { useDispatch, useSelector } from "react-redux";
import QQMapWX from "../../utils/qqmap-wx-jssdk.min.js";

import './index.scss';

const Navigators = (props) => {
    const { jump } = props;
    const dispatch = useDispatch();
    const { positioning: { address } } = useSelector(s => s);

    // 跳转
    const handleJump = () => {
        Taro.navigateTo({
            url: jump.url
        })
    };
    // 实例化 腾讯地图
    const qqmapsdk = new QQMapWX({
        key: "4JJBZ-Y5WKU-SB2VY-BJURA-KME77-LXBBE",
    });
    // 手动获取地理位置
    const handleGetPosition = () => {
        Taro.getSetting({
            success: function (res) {
                if (res.authSetting["scope.userLocation"] === true) {
                    // 已经授权
                    Taro.chooseLocation({
                        latitude: address.latitude,
                        longitude: address.longitude,
                        success: (res) => {
                            qqmapsdk.reverseGeocoder({
                                location: {
                                    latitude: res.latitude,
                                    longitude: res.longitude,
                                },
                                success: function (result) {
                                    console.log("省市区:", result.result.address);
                                    dispatch(set_address({
                                        latitude: res.latitude,
                                        longitude: res.longitude,
                                        address: result.result.address,
                                        name: res.name,
                                    }));
                                    Taro.showToast({
                                        title: "获取成功",
                                        icon: "success",
                                        duration: 1000,
                                    });
                                },
                            });
                        },
                        fail: function () {
                            Taro.showToast({
                                title: "获取失败",
                                icon: "none",
                                duration: 1000,
                            });
                        },
                    });
                } else if (
                    res.authSetting["scope.userLocation"] === undefined ||
                    res.authSetting["scope.userLocation"] === false
                ) {
                    // 未授权
                    Taro.showModal({
                        title: "请求授权当前位置",
                        content: "需要获取您的地理位置，请确认授权",
                        success: function (res) {
                            if (res.cancel) {
                                // 取消授权
                                Taro.showToast({
                                    title: "拒绝授权",
                                    icon: "none",
                                    duration: 1300,
                                });
                            } else {
                                Taro.openSetting({
                                    // 授权获取地理位置
                                    success: function (dataAu) {
                                        // 同意
                                        if (dataAu.authSetting["scope.userLocation"] == true) {
                                            Taro.showToast({
                                                title: "授权成功",
                                                icon: "success",
                                                duration: 1000,
                                            });
                                            Taro.chooseLocation({
                                                // 选择地理位置
                                                success: function (res) {
                                                    dispatch(set_address({
                                                        latitude: res.latitude,
                                                        longitude: res.longitude,
                                                        address: res.address,
                                                        name: res.name,
                                                    }));
                                                    Taro.showToast({
                                                        title: "获取成功",
                                                        icon: "success",
                                                        duration: 1000,
                                                    });
                                                },
                                                fail: function () {
                                                    // 定位失败
                                                    Taro.showToast({
                                                        title: "定位失败",
                                                        icon: "none",
                                                        duration: 1300,
                                                    });
                                                },
                                            });
                                        } else {
                                            // 不同意
                                            Taro.showToast({
                                                title: "授权失败",
                                                icon: "none",
                                                duration: 1000,
                                            });
                                        }
                                    },
                                });
                            }
                        },
                    });
                }
            },
        });
    };
    return (<View class="navgatorBar">
        <View class="header">八维社区团购团长端</View>
        <View class="address">
            <Text onClick={handleGetPosition}>
                位置：{address.address}
            </Text>
            <View
                className="navigators"
            >{jump.title}</View
            >
        </View >
    </View >)
};

Navigators.defaultProps = {
    jump: {
        title: "",
        url: ""
    }
}
Navigators.propTypes = {
    jump: PropTypes.object
}


export default Navigators;